﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using pygs.Lib.GPSIntermediate;
using pygs.Classes;

namespace pygs.Locator
{
    class GPSILocator : CustomLocator
    {
        private Gps _gps;
        protected Gps gps { get { return _gps; } }

        protected override void InitilizeComponent()
        {
            base.InitilizeComponent();
            _gps = new Gps();
        }

        protected override void FinalizeComponent()
        {
            _gps = null;
            base.FinalizeComponent();
        }

        protected override void DoStartUp()
        {
            base.DoStartUp();
            gps.LocationChanged += LocationChanged;
        }

        protected override void DoShutDown()
        {
            gps.LocationChanged -= LocationChanged;
            base.DoShutDown();
        }

        private System.Object lockThis = new System.Object();
        protected List<Location> locations = new List<Location>();

        protected Location LocationFromPosition(GpsPosition position)
        {
            Location location = new Location();
            if (position.TimeValid)
                location.Time = position.Time;
            if (position.SpeedValid)
                location.Speed = position.Speed;
            if (position.EllipsoidAltitudeValid)
                location.EllipsoidAltitude = position.EllipsoidAltitude;
            if (position.SeaLevelAltitudeValid)
                location.SeaLevelAltitude = position.SeaLevelAltitude;
            if (position.LatitudeValid)
                location.Latitude = position.Latitude;
            if (position.LongitudeValid)
                location.Longitude = position.Longitude;
            if (position.HeadingValid)
                location.Heading = position.Heading;
            return location;
        }

        protected void LocationChanged(object sender, LocationChangedEventArgs args)
        {
            Location location = LocationFromPosition(args.Position);
            lock (lockThis)
            {
                locations.Add(location);
            }            
        }

        public override void ProccessNewLocations()
        {
            while (locations.Count > 0)
            {
                Location location = null;
                lock (lockThis)
                {
                    if (locations.Count > 0)
                    {
                        location = locations.First();
                        locations.RemoveAt(0);
                    }
                }
                if (location != null)
                    DoNewLocation(location);
            }
        }

        protected override Location GetLastLocation()
        {
            if (locations.Count > 0)
                return locations.Last();
            return null;
        }

        public override Location GetCurrentLocation()
        {
            return LocationFromPosition(gps.GetPosition());
        }

        public override void Start()
        {
            gps.Open();
        }

        public override void Stop()
        {
            gps.Close();
        }

        protected override LocatorStatus GetStatus()
        {
            //gps.GetDeviceState()
            return null;
        }
    }
}
